package com.example.hotel_lxy.ui.dialog;

import android.app.Dialog;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.util.Log;
import android.view.Gravity;
import android.view.View;
import android.view.ViewGroup;
import android.view.WindowManager;
import android.widget.ImageView;
import android.widget.PopupWindow;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;
import androidx.viewpager.widget.PagerAdapter;

import com.example.hotel_lxy.R;
import com.example.hotel_lxy.bean.BeanBean;
import com.example.hotel_lxy.bean.DialogAnniversaryBean;
import com.example.hotel_lxy.bean.EventBusBean;
import com.example.hotel_lxy.bean.FoodDialogBean;
import com.example.hotel_lxy.net.Api;
import com.example.hotel_lxy.ui.activity.LoginActivity;
import com.example.hotel_lxy.ui.main.CalendarActivity;
import com.example.hotel_lxy.utils.SPUtils;
import com.ysxsoft.common_base.base.RBaseAdapter;
import com.ysxsoft.common_base.base.RViewHolder;
import com.ysxsoft.common_base.utils.DisplayUtils;
import com.ysxsoft.common_base.utils.JsonUtils;
import com.ysxsoft.common_base.utils.ToastUtils;
import com.zhy.http.okhttp.OkHttpUtils;
import com.zhy.http.okhttp.callback.StringCallback;

import org.greenrobot.eventbus.EventBus;
import org.greenrobot.eventbus.Subscribe;
import org.greenrobot.eventbus.ThreadMode;

import java.math.BigDecimal;
import java.math.RoundingMode;
import java.util.List;

import okhttp3.Call;


/**
* 已选菜品
* create by Sincerly on 9999/9/9 0009
**/
public class SelectedFoodDialog extends Dialog {
    private Context mContext;
    private static  String id;
    private RecyclerView rv_food;
    private TextView text_money;
    private String money_int="0";
    public SelectedFoodDialog(@NonNull Context context, int themeResId) {
        super(context, themeResId);
        this.mContext = context;
//        init();
    }

    private View init() {
        View view = View.inflate(mContext, R.layout.dialog_selectedfood, null);
        rv_food=view.findViewById(R.id.rv_food);
        text_money=view.findViewById(R.id.text_money);
        showDataFood();

        return view;
    }

    private void showDataFood() {
        OkHttpUtils.post()
                .url(Api.POST_MENU)
                .addHeader("flag", SPUtils.get(mContext, "token", "").toString())
                .addParams("did", id)
                .tag(this)
                .build()
                .execute(new StringCallback() {
                    @Override
                    public void onError(Call call, Exception e, int id) {
                        Log.e("TAG", "添加菜单onError   " + e.getMessage());
                    }

                    @Override
                    public void onResponse(String response, int id) {
                        Log.e("TAG", "添加菜单onResponse   " + response);
                        BeanBean beanBean= JsonUtils.parseByGson(response, BeanBean.class);
                        if(beanBean.getCode().equals("4")){
                            SPUtils.put(mContext,"token","");
                            Intent intent = new Intent(mContext, LoginActivity.class).
                                    setFlags(Intent.FLAG_ACTIVITY_CLEAR_TASK | Intent.FLAG_ACTIVITY_NEW_TASK);
                            mContext.startActivity(intent);
                            return;
                        }
                        FoodDialogBean foodDialogBean = JsonUtils.parseByGson(response, FoodDialogBean.class);
                        if (beanBean.getCode().equals("0")) {
                            rv_food.setLayoutManager(new LinearLayoutManager(mContext));
                            RBaseAdapter<FoodDialogBean.DataBeanX.ListBean> rBaseAdapter=new RBaseAdapter<FoodDialogBean.DataBeanX.ListBean>(mContext,
                                    R.layout.item_food, foodDialogBean.getData().getList()) {
                                @Override
                                protected void fillItem(RViewHolder holder, FoodDialogBean.DataBeanX.ListBean item, int position) {
                                    TextView text_name=holder.itemView.findViewById(R.id.text_name);
                                    text_name.setText(item.getName());
                                    RecyclerView rv_food_item=holder.itemView.findViewById(R.id.rv_food_item);
                                    rv_food_item.setLayoutManager(new LinearLayoutManager(mContext));
                                    RBaseAdapter<FoodDialogBean.DataBeanX.ListBean.DataBean> rBaseAdapter1=new RBaseAdapter<FoodDialogBean.DataBeanX.ListBean.DataBean>(mContext,
                                            R.layout.item_item_food,item.getData()) {
                                        @Override
                                        protected void fillItem(RViewHolder holder, FoodDialogBean.DataBeanX.ListBean.DataBean item, int position) {
                                            TextView name=holder.itemView.findViewById(R.id.name);
                                            TextView num=holder.itemView.findViewById(R.id.num);
                                            TextView money=holder.itemView.findViewById(R.id.money);
                                            name.setText(""+item.getName());
                                            num.setText(""+item.getNum());
                                            money.setText("￥ "+item.getMoney());
                                            if(item.getNum()>1){
                                                BigDecimal num1 =new BigDecimal(item.getMoney()+"");
                                                BigDecimal num2 =new BigDecimal(item.getNum()+"");
                                                BigDecimal num3 =new BigDecimal(money_int+"");
//                                             long i= Float.valueOf(item.getMoney()+"")*;
                                                money_int=num1.multiply(num2).add(num3).setScale(2, RoundingMode.HALF_UP)+"";
                                            }else{
                                                BigDecimal num2 = new BigDecimal(""+money_int);
                                                BigDecimal num1 =new BigDecimal(item.getMoney()+"");
//                                                money_int=money_int+aFloat;
                                                money_int=num2.add(num1).setScale(2,RoundingMode.HALF_UP)+"";
                                            }
                                            text_money.setText("合计："+money_int);
                                        }

                                        @Override
                                        protected int getViewType(FoodDialogBean.DataBeanX.ListBean.DataBean item, int position) {
                                            return 0;
                                        }
                                    };
                                    rv_food_item.setAdapter(rBaseAdapter1);
                                }

                                @Override
                                protected int getViewType(FoodDialogBean.DataBeanX.ListBean item, int position) {
                                    return 0;
                                }
                            };
                            rv_food.setAdapter(rBaseAdapter);

                        }
                    }
                });
    }


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setCanceledOnTouchOutside(true);
        setContentView(init());
    }

    public void showDialog() {
        if (!isShowing()) {
            show();
            WindowManager.LayoutParams lp = getWindow().getAttributes();
//            lp.height = DisplayUtils.getDisplayWidth(mContext) * 5 / 5;
            lp.width = DisplayUtils.getDisplayWidth(mContext) * 8 / 10;
            lp.height = WindowManager.LayoutParams.WRAP_CONTENT;
            getWindow().setAttributes(lp);
            getWindow().setGravity(Gravity.CENTER);
        }
    }


    public static SelectedFoodDialog show(Context context, OnDialogClickListener listener, String ids) {
        SelectedFoodDialog dialog = new SelectedFoodDialog(context, R.style.BottomDialogStyle);
        dialog.setListener(listener);
        id=ids;
        dialog.showDialog();
        return dialog;
    }

    public OnDialogClickListener listener;

    public SelectedFoodDialog setListener(OnDialogClickListener listener) {
        this.listener = listener;
        return this;
    }
    public interface OnDialogClickListener {
        /**
         * 点击确定按钮事件
         */
        public void onPositiveClick(String s);
    }

}